#include "HomeSceneContent.h"
#include "../../model/proxy/UserProxy.h"
#include "scene/PacketScene.h"

HomeSceneContent::HomeSceneContent(void)
	:mAttack(NULL),mDefense(NULL),mLife(NULL)
{
	
}

HomeSceneContent::~HomeSceneContent(void)
{
	CC_SAFE_RELEASE(mAttack);
	CC_SAFE_RELEASE(mDefense);
	CC_SAFE_RELEASE(mLife);
}

bool HomeSceneContent::init()
{
	return true;
}

cocos2d::SEL_MenuHandler HomeSceneContent::onResolveCCBCCMenuItemSelector( cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onHelmClick", HomeSceneContent::onHelmClick);
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onNecklaceClick", HomeSceneContent::onNecklaceClick);
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onWeaponClick", HomeSceneContent::onWeaponClick);
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onClothClick", HomeSceneContent::onClothClick);
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onRingClick", HomeSceneContent::onRingClick);
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onShoesClick", HomeSceneContent::onShoesClick);

	return NULL;
}

cocos2d::extension::SEL_CCControlHandler HomeSceneContent::onResolveCCBCCControlSelector( cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onAttributeBtnClick", HomeSceneContent::onAttributeBtnClick);
	return NULL;
}

bool HomeSceneContent::onAssignCCBMemberVariable( cocos2d::CCObject * pTarget, cocos2d::CCString * pMemberVariableName, cocos2d::CCNode * pNode )
{
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAttack", CCLabelTTF *, this->mAttack);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mDefense", CCLabelTTF *, this->mDefense);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mLife", CCLabelTTF *, this->mLife);

	return true;
}

void HomeSceneContent::onHelmClick( cocos2d::CCObject * pSender )
{
	PacketScene::msCurTab = PACK_TAB_EQUIP;
	Framework::getSingletonPtr()->changeState("PacketScene");
}

void HomeSceneContent::onNecklaceClick( cocos2d::CCObject * pSender )
{
	PacketScene::msCurTab = PACK_TAB_EQUIP;
	Framework::getSingletonPtr()->changeState("PacketScene");
}

void HomeSceneContent::onWeaponClick( cocos2d::CCObject * pSender )
{
	PacketScene::msCurTab = PACK_TAB_EQUIP;
	Framework::getSingletonPtr()->changeState("PacketScene");
}

void HomeSceneContent::onClothClick( cocos2d::CCObject * pSender )
{
	PacketScene::msCurTab = PACK_TAB_EQUIP;
	Framework::getSingletonPtr()->changeState("PacketScene");
}

void HomeSceneContent::onRingClick( cocos2d::CCObject * pSender )
{
	PacketScene::msCurTab = PACK_TAB_EQUIP;
	Framework::getSingletonPtr()->changeState("PacketScene");
}

void HomeSceneContent::onShoesClick( cocos2d::CCObject * pSender )
{
	PacketScene::msCurTab = PACK_TAB_EQUIP;
	Framework::getSingletonPtr()->changeState("PacketScene");
}

void HomeSceneContent::refreshEquip()
{
    EquipList& equipList = ItemProxy::getSingletonPtr()->getEquipList();
    EquipList::iterator iter = equipList.begin();
    while(iter != equipList.end())
    {
        if((*iter)->state == 1)
            putOnEquip((*iter));
        iter++;
    }
}

void HomeSceneContent::refresh()
{
	UserVO& userVO = UserProxy::getSingletonPtr()->userVO;
	mAttack->setString(fcs("%d-%d",userVO.atkMin,userVO.atkMax));
	mDefense->setString(fcs("%d-%d",userVO.atkMin,userVO.defMax));
	mLife->setString(fcs("%d",userVO.life));
	refreshEquip();
}

void HomeSceneContent::onEnter()
{
    CCNode::onEnter();    
    CCArray* nameList = CCArray::create(ccs(kNCRegist),NULL);
	RegisterObservers(this, nameList, callfuncO_selector(HomeSceneContent::_onNotification));
	refresh();
}

void HomeSceneContent::onExit()
{
	CCNode::onExit();
}

void HomeSceneContent::putOnEquip( EquipInfo* equipInfo )
{
	std::map<EquipPart, CCPoint> point;
	point[EQUIP_PART_HELM] = ccp(35, 328);
	point[EQUIP_PART_NECKLACE] = ccp(90, 328);
	point[EQUIP_PART_WEAPON] = ccp(145, 328);
	point[EQUIP_PART_CLOTH] = ccp(33, 266);
	point[EQUIP_PART_RING] = ccp(90, 266);
	point[EQUIP_PART_SHOES] = ccp(145, 266);
	EquipPart part = ItemProxy::getSingletonPtr()->getEquipPart(equipInfo->id);
	Item* item = mEquipList[part];
	if (item == NULL)
	{
		item = new Item();
		item->autorelease();
		item->setPosition(ccp(point[part].x, point[part].y));
        item->setScale(0.2);
		addChild(item);
	}
    item->itemID = equipInfo->id;
	item->setIcon();
}

void HomeSceneContent::_onNotification(CCObject* object)
{
    CCLog("HomeSceneContent::_onNotification[%x]", this);
    
	NotificationObserver* notification = (NotificationObserver*)object;
    std::string name = std::string(notification->getName());
	if (name == kNCRegist || name == kNCDungeonExplore || name == kNCSellEquipage)
	{
        refresh();
	}
}

void HomeSceneContent::onAttributeBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	Framework::getSingletonPtr()->changeState("PlayerDetailScene");
}
